<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 4, 2013
 * Time: 4:05:34 PM
 * To change this template use File | Settings | File Templates.
 */

class Institute {
    private $db;
    private $id;
    private $instituteName;
    private $instituteCode;
    private $instituteAddress;
    private $phone;
    private $fax;
    private $email;
    private $contactPerson;
    private $category;
    private $parentInstitute;
    private $regDate;
    private $hasSubInst;
    private $headName;
    private $headAddress;
    private $headEmail;
    private $headPhone;
    private $rpoName;
    private $rpoAddress;
    private $rpoPhone;
    private $rpoEmail;
    private $status;
    private $isDelete;
    private $version;
    private $instituteMenu;

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
        $this->setInstituteArray();
    }

    public function setInstituteArray() {
        $sql = "SELECT id,name from aea_institute order by id asc";
        $dep_code = $this->db->Execute($sql);
        $this->instituteMenu = $dep_code->GetAssoc();

    }

    public function GetInstituteListMenu() {
        //$sql = 'select name,id from aea_department';
        $sql = 'select name,id from aea_institute where  (parent_ins_id=0 or has_sub_ins=1)  and is_delete=0';
        $sectionMenuList = $this->db->Execute($sql);
        return $sectionMenuList->GetMenu2('searchCriteria.institute', '', ':--All select--', '', '', 'id="searchCriteria.institute" onchange="doSearch(event)" style="width:150px;"');
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $institute = new InstituteAR();
            if ($this->id != '' AND $this->id != NULL) {
                $institute->Load("id=$this->id");
            } else {
                $institute->create_time = date('Y-m-d H:i:s', time());
                $institute->create_user_id = $_SESSION['user_id'];
                $institute->is_delete = 0;
            }
            $institute->code = $this->instituteCode;
            $institute->name = $this->instituteName;
            $institute->address = $this->instituteAddress;
            $institute->phone = $this->phone;
            $institute->email = $this->email;
            $institute->fax = $this->fax;
            $institute->contact_person = $this->contactPerson;
            $institute->category_id = $this->category;
            $institute->parent_ins_id = $this->parentInstitute;
            $institute->reg_date = $this->regDate;
            $institute->has_sub_ins = $this->hasSubInst;
            $institute->head_name = $this->headName;
            $institute->head_address = $this->headAddress;
            $institute->head_email = $this->headEmail;
            $institute->head_phone = $this->headPhone;
            $institute->rpo_name = $this->rpoName;
            $institute->rpo_address = $this->rpoAddress;
            $institute->rpo_phone = $this->rpoPhone;
            $institute->rpo_email = $this->rpoEmail;
            $institute->status = $this->status;
            $institute->update_user_id = $_SESSION['user_id'];
            $institute->version = $this->version;
            $i = $institute->save();
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getDepartmentOptions() {
        $sql = 'select name,id,dep_code from aea_department';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                    " . $res->fields['dep_code'] . " : " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getEmpCategoryOptions() {
        $sql = 'select name,id,code from aea_employ_category where is_delete=0';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                    " . $res->fields['code'] . "  " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getSectionOptions($insId) {
        $sql = "select id, name from aea_institute where parent_ins_id ='$insId' and is_delete=0";
        $res = $this->db->Execute($sql);
        if ($res) {
            $optionString .= "<option value='0'> --Select Section--   </option>";
            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                    " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getInstituteOptions() {
        $sql = 'select name,id from aea_institute where (parent_ins_id=0 or has_sub_ins=1) and is_delete=0';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                     " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getParentInstituteOptions() {
        $sql = 'select name,id from aea_institute where (parent_ins_id=0 or has_sub_ins=1) and is_delete=0';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                     " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getCategoryOptions() {
        $sql = 'select id,name from aea_institute_category where is_delete=0';
        $res = $this->db->Execute($sql);
        if ($res) {

            while (!$res->EOF) {
                $optionString .= "<option value=" . $res->fields['id'] . ">
                     " . $res->fields['name'] . "</option>";
                $posStart++;
                $res->MoveNext();
            }

        } else {
            $OptionString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $optionString;
    }

    public function getInstitute($instId) {
        $sql = "SELECT  * FROM aea_institute Where id='$instId' LIMIT 1";
        $res = $this->db->Execute($sql);

        return $res;
    }

    public function getGrid($posStart, $count, $name, $status, $sort_by, $sort_dir) {
        $sql = "SELECT id,name,parent_ins_id,address,status FROM aea_institute Where is_delete=0";
        if ($name != '')
            $sql .= " and name like '$name%'";
        if ($status != '')
            $sql .= " and status = '$status'";

        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                    <cell>" . $res->fields['id'] . "</cell>
                    <cell>" . $res->fields['name'] . "</cell>
                    <cell>" . $this->instituteMenu[$res->fields['parent_ins_id']] . "</cell>
                    <cell>" . $res->fields['address'] . "</cell>
                    <cell>" . ($res->fields['status'] == 1 ? "Active" : "Incative") . "</cell>
                    <cell>" . $res->fields['status'] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }

}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class InstituteAR extends ADOdb_Active_Record {
    var $_table = 'aea_institute';

}
